﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using PMS.Domain.AggregatesModel.ProjectAggregate;

namespace PMS.Application.ProjectApp
{
    public class ProjectService : IProjectService
    {
        public readonly IProjectRepository _projectRepository;

        public ProjectService(IProjectRepository projectRepository)
        {
            _projectRepository = projectRepository;
        }

        public async Task AddAuditAdviceAsync(int projectId, int userId, string userName, string message)
        {
            var project = await _projectRepository.GetAsync(projectId);
            project.AddAdvice(userId, userName, message);
            _projectRepository.Update(project);
            await _projectRepository.UnitOfWork.SaveEntitiesAsync();
        }

        public async Task AddConsumableAsync(int projectId, List<ProjectConsumable> consumablies)
        {
            var project = await _projectRepository.GetAsync(projectId);
            project.AddConsumable(consumablies);
            _projectRepository.Update(project);
            await _projectRepository.UnitOfWork.SaveEntitiesAsync();
        }

        public async Task AddContributorAsync(int projectId, List<ProjectContributor> contributorList)
        {
            var project = await _projectRepository.GetAsync(projectId);
            project.AddContributor(contributorList);
            _projectRepository.Update(project);
            await _projectRepository.UnitOfWork.SaveEntitiesAsync();
        }

        public async Task<Project> CreateProjectAsync(Project project)
        {
            _projectRepository.Add(project);         
            await _projectRepository.UnitOfWork.SaveEntitiesAsync();
            return project;
        }

        public async Task RemoveConsuableAsync(int projectId, List<ProjectConsumable> consumablies)
        {
            var project = await _projectRepository.GetAsync(projectId);
            project.RemoveConsumable(consumablies);
            _projectRepository.Update(project);
            await _projectRepository.UnitOfWork.SaveEntitiesAsync();
        }

        public async Task RemoveContributorAsync(int projectId, List<ProjectContributor> contributorList)
        {
            var project = await _projectRepository.GetAsync(projectId);
            project.RemoveContributor(contributorList);
            _projectRepository.Update(project);
            await _projectRepository.UnitOfWork.SaveEntitiesAsync();
        }

        public async Task UpdateConsumableAsync(int projectId, ProjectConsumable projectConsumable)
        {
            var project = await _projectRepository.GetAsync(projectId);
            project.UpdateConsumable(projectConsumable);
            _projectRepository.Update(project);
            await _projectRepository.UnitOfWork.SaveEntitiesAsync();
        }
    }
}
